/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dynamixel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
import jssc.SerialPortList;
import jssc.SerialPortTimeoutException;

/**
 *
 * @author root
 */
public class ComPortFinder {

    private static ArrayList<String> serialPortList = null;

    public static synchronized String findCom(ComPortFindParam cpfp) {
        int i = 0, j = 0;
        String retResult = null;
        int[] comAnswer = {};
        boolean finded_com = false;
        //System.out.println("findCOM");
        if (serialPortList == null) {
            serialPortList = new ArrayList<>(
                    Arrays.asList(SerialPortList.getPortNames()));
        }
        //System.out.println("Size serialList:" + serialPortList.size());
        SerialPort sp = null;
        if (serialPortList != null) {
            while (i < serialPortList.size()) {
                try {
                    //System.out.println("port: " + serialPortList.get(i));
                    sp = new SerialPort(serialPortList.get(i));
                    if (!sp.isOpened()) {
                        sp.openPort();
                        sp.setParams(cpfp.getBaudRate(),
                                cpfp.getDataBits(), cpfp.getStopBits(),
                                cpfp.getParity());
                        j = 0;
                        while (j < 3 && !finded_com) {
                            sp.purgePort(SerialPort.PURGE_RXCLEAR
                                    | SerialPort.PURGE_TXCLEAR);
                            //System.out.println("Send num: " + j);
                            if (cpfp.getSendPckg() != null) {
                                sp.writeIntArray(cpfp.getSendPckg());
                            } else {
                                //System.out.println("Send array is null");
                            }
                            try {
                                comAnswer = sp.readIntArray(cpfp.getBytesCnt(),
                                        cpfp.getTimeOut());
                                System.out.println("comAnswer: " + Arrays.toString(comAnswer));
                                if (cpfp.isFinded(comAnswer)) {
                                    finded_com = true;
                                    retResult = serialPortList.get(i);
                                    serialPortList.remove(i);
                                }
                                j++;
                            } catch (SerialPortTimeoutException ex) {
                                //System.out.println("Time out exceptuion");
                                j++;
                            }
                        }
                        sp.closePort();
                    } else {
                        //System.out.println("Serial port is already opened.");
                    }

                    i++;
                } catch (SerialPortException ex) {
                    System.out.println("Serial port is not availiable: "
                            + "" + sp.getPortName());
                    i++;
                }
            }

        } else {
            //System.out.println("Error.There are no availiable serial ports.");
        }
        
        //System.out.println("find return: "+retResult);
        return retResult;
    }
}
